/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.manto;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.manto.RolForm;
import com.uca.sisbec.helpers.manto.RolHelper;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Transaction;

/**
 *
 * @author Juank
 */
public class RolAction extends HibernateAction {
    public ActionForward init(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RolForm rf = (RolForm) form;
        rf.setDisponibles(new int[0]);
        rf.setSeleccionadas(new int[0]);
        setOptionsList(request, rf);
        request.setAttribute("save", "new");
        return mapping.findForward("success");
    }
    
    public ActionForward agregarRol(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RolForm rf = (RolForm) form;
        RolHelper helper = new RolHelper(getSession(request));
        helper.insertarRol(rf);
        helper.agregarModulos(rf);
        setOptionsList(request, rf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward modificarRol(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RolForm rf = (RolForm) form;
        RolHelper helper = new RolHelper(getSession(request));
        helper.modificarRol(rf);
        /* Reinicio de la transaccion */
        getTx(request).commit();
        Transaction tx = getSession(request).beginTransaction();
        request.getSession().setAttribute("_TRX_HIB", tx);
        /* */
        helper.agregarModulos(rf);
        setOptionsList(request, rf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }
    
    public ActionForward seleccionarRol(ActionMapping mapping, ActionForm form, 
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        RolForm rf = (RolForm) form;
        RolHelper helper = new RolHelper(getSession(request));
        helper.setRolForm(rf);
        setOptionsList(request, rf);
        request.setAttribute("save", "update");
        return mapping.findForward("success");
    }

    private void setOptionsList(HttpServletRequest request, RolForm rf) {
        RolHelper helper = new RolHelper(getSession(request));
        request.setAttribute("rolesList", helper.getRolesList());
        request.setAttribute("availableList", helper.getAvailableModulos(rf.getCodigo()));
        request.setAttribute("selectedList", helper.getSelectedModulos(rf.getCodigo()));
    }
}
